package com.woniuxy.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.commons.util.RedisKey;
import com.woniu.zsd.commons.core.exception.ZsdException;
import com.woniuxy.order.entity.WasteType;
import com.woniuxy.order.mapper.WasteTypeMapper;
import com.woniuxy.order.service.IWasteTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import zsd.waste.form.AddWasteTypeForm;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-05-13
 */
@Service
public class WasteTypeServiceImpl extends ServiceImpl<WasteTypeMapper, WasteType> implements IWasteTypeService {
    @Resource
    private WasteTypeMapper wasteTypeMapper;
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public int insertWasteType(AddWasteTypeForm awtf) {
        // 1. 检查废品类型名称是否重复
        QueryWrapper<WasteType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("waste_type_name", awtf.getWasteTypeName());
        WasteType existing = wasteTypeMapper.selectOne(queryWrapper);
        if (existing != null) {
            throw new ZsdException("废品类型名称已存在", 500);
        }
        WasteType entity = new WasteType();
        entity.setWasteTypeName(awtf.getWasteTypeName());
        entity.setPrice(awtf.getPrice());
        entity.setPriceUtil(awtf.getPriceUtil());

        // 插入数据
        return wasteTypeMapper.insert(entity);
    }

    @Override
    public List<WasteType> selWasteType() {
        // 使用 MyBatis-Plus 查询所有
        List<WasteType> wasteTypeList = wasteTypeMapper.selectList(null);

        // 存入 Redis Hash
        for (WasteType type : wasteTypeList) {
            redisTemplate.opsForHash().put(
                    RedisKey.WASTE_TYPE_KEY(),
                    type.getId().toString(),
                    type.getWasteTypeName()
            );
        }

        return wasteTypeList;
    }

    @Override
    public int updateWasteType(WasteType wasteType) {
        QueryWrapper<WasteType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("waste_type_name", wasteType.getWasteTypeName());
        if (wasteTypeMapper.selectOne(queryWrapper) == null)throw new ZsdException("该名称不存在", 500);
        return wasteTypeMapper.update(wasteType, queryWrapper);
    }

    @Override
    public int deleteWasteType(Integer id) {
        QueryWrapper<WasteType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        WasteType wasteType = wasteTypeMapper.selectOne(queryWrapper);
        if (wasteType == null) throw new ZsdException("不存在该废品", 500);
        return wasteTypeMapper.delete(queryWrapper);
    }

    @Override
    public WasteType getWasteTypeById(Integer id) {
        QueryWrapper<WasteType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        WasteType wasteType = wasteTypeMapper.selectOne(queryWrapper);
        if (wasteType==null)throw new ZsdException("不存在该废品", 500);
        return wasteType;

    }

    @Override
    public List<WasteType> getWasteTypeByName(String name) {
        QueryWrapper<WasteType> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("waste_type_name", name);
        if (!wasteTypeMapper.selectList(queryWrapper).isEmpty()) {
            return wasteTypeMapper.selectList(queryWrapper);
        }else throw new ZsdException("不存在该废品", 500);
    }

    @Override
    public List<WasteType> getTop5SearchedWasteTypes() {
        String key = RedisKey.WASTE_TYPE_COUNT_KEY();
        // 获取查询次数最多的五个废品类型名称
        Set<String> top5Names = redisTemplate.opsForZSet().reverseRange(key, 0, 4);

        // 根据名称查询废品类型详情
        List<WasteType> top5WasteTypes = new ArrayList<>();
        if (top5Names != null) {
            for (String name : top5Names) {
                WasteType wasteType = wasteTypeMapper.getWasteTypeByName(name);
                if (wasteType != null) {
                    top5WasteTypes.add(wasteType);
                }
            }
        }

        return top5WasteTypes;
    }
}
